/*
 * fasthands.c
 *
 * Created: 10/12/2011 11:14:22 AM
 *  Author: Chaz
 */ 

#include <avr/io.h>
#include <D:\Program Files (x86)\Atmel\AVR Studio 5.0\AVR Toolchain\lib\gcc\avr32\4.4.3\include\c++\tr1\stdlib.h>
#define sensorpin PIND	// sensor port reg
#define sensorddr DDRD		//sensor port dir reg
#define sensorport PORTD		//sensor port output reg
#define sensorpinnum 0		// define pin number

#define sensorpulsetime 100	//time to pulse sensor for
#define distthresh 100		//distance threshold to be considered hand is there
#define idlereadtime 0xff	//defines the time between sensor reads when no hand is detected

#define actuatorddr DDRD	// actuator dir reg
#define actuatorport PORTD	//actuator port output reg
#define actuatorpinnum 1 // actuator pin number
#define actuatorontime 10	//the amount of time the actuator is on

int main(void)
{
	short int dist;		//distance value
	short int randseed=0;	//random seed value
	short int count = 0;
	short int delaytime = 0;
	
	randseed = readsensor();
	srand(randseed);			// seed random with sensor reading
    while(1)
    {   
		dist = readsensor();
		if (dist > distthresh)		// if no hand is detected
			{
				count = 0;			// set count =0
				do 
				{
					count = count + 1;		// and then wait for idlereadtime counts to elapse and then read again
				} while (count != idlereadtime);
			}
		else							//otherwis
			{
				delaytime = rand();	// get random value
				count = 0;
				do 
				{
					count = count +1;				//and then pause for that amount of time
				} while (count != delaytime);
				
				dist = readsensor();
				if (dist > distthresh)	// if hand is still there
					{
						actuatorport = actuatorport | (1<<actuatorpinnum);	// turn actuator output on
				
						count = 0;			// reset count
				
						do 
						{
							count = count+1;		// leave actuator on for a while
						} while (count < actuatorontime);
				
						actuatorport = actuatorport | ~(1<< actuatorpinnum); //then shut it off			
					}					
			}	// and then go back and read sensor again 
    }
	
}

int readsensor(void)	// read sensor function
{
	short int count = 0;	// count timer variable
	short int dist = 0;	// distance value
	sensorport= sensorport & ~(1<<sensorpinnum);	//ensure output is low by clearing port pin
	sensorddr = sensorddr | (1<<sensorpinnum);		// set direction to output
	sensorport = sensorport | (1<<sensorpinnum);	// drive output high
	do				//wait for sensorpuletime clock counts 
	{
		count= count + 1;
	} while (count != sensorpulsetime);
	sensorport= sensorport & ~(1<<sensorpinnum);	// set sensor pin low
	sensorddr = sensorddr & ~(1<<sensorpinnum);		// and then make sensor pin an input
	count=0;
	do
	{
		count = sensorpin;								// hang out until first clock of returning pulse
		count = sensorpin & (1<<sensorpinnum);			// clear all bits except sensor pin bit
	} while (count != (1<<sensorpinnum));		// while senor pin is low 
	
	count = 0;
	
	do 
	{
		dist = dist + 1;						// increment count
		count = sensorpin & (1<<sensorpinnum);	//clear all bits except for sensor pin bit
	} while (count == (1<<sensorpinnum));		// while sensor is high
	
	return(count);
			
}